package com.cb.controller.admin.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.command.QueryFilter;
import com.cb.core.command.QueryResult;
import com.cb.core.controller.BaseListController;
import com.cb.domain.model.system.AppResource;
import com.cb.domain.model.system.AppRole;
import com.cb.domain.model.system.SystemUser;
import com.cb.domain.service.system.AppRoleManager;
import com.cb.domain.service.system.SystemUserManager;

/**
 * 
 * @Author author
 * @Vesion 1.0
 * Create Time:
 * <p><b>Class Description:</b>[TODO]<p>
 */
public class SystemUserController extends BaseListController {
	
	@Autowired
	private SystemUserManager systemUserManager;
	
	@Autowired
	private AppRoleManager appRoleManager;

	
	/**对象列表页，提供最基本的分页显示
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		QueryResult systemUsers=systemUserManager.getAll(new QueryFilter(request));
		
		return new ModelAndView("admin/system/systemUserList","systemUsers",systemUsers);
	}
	/**
	 * 跳转用户授权
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView grantUser(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String systemUserId=request.getParameter("systemUserId");
		SystemUser systemUser= systemUserManager.get(Long.valueOf(systemUserId));

		List<AppRole> appRoles= appRoleManager.getAll();
		
		return new ModelAndView("admin/system/grantUser","systemUser",systemUser).addObject("appRoles", appRoles);
	}
	
	/**
	 * 保存用户授权
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView doGrant(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String [] roleIds=request.getParameterValues("roleId");
		String systemUserId=request.getParameter("systemUserId");
		SystemUser systemUser=systemUserManager.get(Long.valueOf(systemUserId));
		
		if(roleIds!=null){
			systemUser.getRoles().clear();
			for (int i = 0; i < roleIds.length; i++) {
					AppRole appRole=appRoleManager.get(new Long(roleIds[i]));
					systemUser.getRoles().add(appRole);
			}}
		systemUserManager.save(systemUser);
		
		return new ModelAndView("redirect:/admin/system/systemUsers.do");
	}
	
	
	
	/**
	 * 批量删除，通过页面传回对象的主键数组进行逐条删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView multiDelete(HttpServletRequest request,	HttpServletResponse response) {
		String[] systemUserIds=request.getParameterValues("systemUserId");
		if(systemUserIds!=null){
			for(String systemUserId:systemUserIds){
				systemUserManager.remove(new Long(systemUserId));
			}
			saveMessage(request, getText("systemUser.mutiDeleted"));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		return new ModelAndView(getSuccessView());
	}
	
	/**
	 * 删除单个实体对象
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView delete(HttpServletRequest request,	HttpServletResponse response){
		String systemUserId=request.getParameter("systemUserId");
		if(StringUtils.isNotEmpty(systemUserId)){
			SystemUser systemUser=systemUserManager.get(new Long(systemUserId));
			systemUserManager.remove(systemUser);
			saveMessage(request,getText("systemUser.deleted",new Object[]{systemUser.getSystemUserId()}));
		}
		String url=request.getHeader("Referer");
		if(StringUtils.isNotEmpty(url)){
			return new ModelAndView("redirect:"+url);
		}
		return new ModelAndView(getSuccessView());
	}
	
	/**
	 * 查看实体对象详细信息
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView viewDetail(HttpServletRequest request,	HttpServletResponse response){
		String systemUserId=request.getParameter("systemUserId");
		SystemUser systemUser=null;
		if(StringUtils.isNotEmpty(systemUserId)){
			systemUser=systemUserManager.get(new Long(systemUserId));
		}
		return new ModelAndView("admin/system/systemUserDetail","systemUser",systemUser);
	}
}
